/**
 * controller: distributor
 *
 * @author    Dmitry Ustimov
 * @date      March 2012
 *
 */
package com.gr

class DistributorController {
	
	def DistributorService

    def getlist = {
		println(params)
		if (params.get("node") == "CompanyTreeRoot") {
			redirect(controller: "division", action: "getlist")
		}
		else if (params.get("node") =~ "Division_") {
			def dbid = DistributorService.nodeIdToDBId(params.get("node"))
			redirect(controller: "project", action: "getlist", params: [id:dbid])
		}
		else if (params.get("node") =~ "Project_") {
			def dbid = DistributorService.nodeIdToDBId(params.get("node"))
			redirect(controller: "person", action: "getlist", params: [id:dbid])
		}	
	}
	
	def getElement = {
		println(params)
		if (params.get("node") == "CompanyTreeRoot") {
			def dbid = 1;
			redirect(controller: "company", action: "getElement", params: [id:dbid])
		}			
		else if (params.get("node") =~ "Division_") {
			def dbid = DistributorService.nodeIdToDBId(params.get("node"));
			redirect(controller: "division", action: "getElement", params: [id:dbid])
		}
		else if (params.get("node") =~ "Project_") {
			def dbid = DistributorService.nodeIdToDBId(params.get("node"));
			redirect(controller: "project", action: "getElement", params: [id:dbid])
		}
		else {
			def dbid = params.get("node")
			redirect(controller: "person", action: "getElement", params: [id:dbid])
		}			
	}
	
	def addElement = {
		println(params)		
		if (params.get("parentNode") == "CompanyTreeRoot") {
			def parentNode = 1;
			params.put('companyTo', parentNode)
			redirect(controller: "division", action: "addElement", params: params)
		}
		else if (params.get("parentNode") =~ "Division_") {
			def parentNode = DistributorService.nodeIdToDBId(params.get("parentNode"))
			params.put('divisionTo', parentNode)
			redirect(controller: "project", action: "addElement", params: params)
		}
		else if (params.get("parentNode") =~ "Project_") {
			def parentNode = DistributorService.nodeIdToDBId(params.get("parentNode"))
			params.put('projectTo', parentNode)
			redirect(controller: "person", action: "addElement", params: params)
		}
	}
	
	def editElement = {
		println(params)
		if (params.get("node") != "CompanyTreeRoot") {
			
			if (params.get("node") =~ "Division_") {
				def dbid = DistributorService.nodeIdToDBId(params.get("node"))
				params.put('id', dbid)
				params.put('companyTo', 1)
				redirect(controller: "division", action: "editElement", params: params)
			}
			else if (params.get("node") =~ "Project_") {
				def dbid = DistributorService.nodeIdToDBId(params.get("node"))
				params.put('id', dbid)	
				def divTo = Division.findByTitle(params.get("divisionTo"))
				params.put('divisionTo', divTo.id)
				redirect(controller: "project", action: "editElement", params: params)
			}
			else {
				def dbid = params.get("node")
				params.put('id', dbid)
				def projTo = Project.findByTitle(params.get("projectTo"))
				params.put('projectTo', projTo.id)
				redirect(controller: "person", action: "editElement", params: params)
			}			
		}
	}
	
	def deleteElement = {
		println(params)
		if (params.get("node") != "CompanyTreeRoot") {
			
			if (params.get("node") =~ "Division_") {
				def dbid = DistributorService.nodeIdToDBId(params.get("node"))
				redirect(controller: "division", action: "deleteElement", params: [id:dbid])
			}
			else if (params.get("node") =~ "Project_") {
				def dbid = DistributorService.nodeIdToDBId(params.get("node"))
				redirect(controller: "project", action: "deleteElement", params: [id:dbid])
			}
			else {
				def dbid = params.get("node")
				redirect(controller: "person", action: "deleteElement", params: [id:dbid])
			}			
		}
	}
}